package com.zhht.htzx.ui;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.binding_state.BR;
import com.nsgf.library.data.bean.OrderListBean;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.utils.ToastUtils;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhht.htzx.R;
import com.zhht.htzx.adapter.OrderBusinessAdapter;
import com.zhht.htzx.databinding.ActivityBusinessOrderBinding;

import java.util.List;

/**
 * 订单列表Activity
 */
public class OrderBusinessListActivity extends BaseActivity {

    private StateHolder mStates;

    private OrderBusinessAdapter orderAdapter;
    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(StateHolder.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    private ActivityBusinessOrderBinding mBinding;

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        orderAdapter = new OrderBusinessAdapter(this);
        return new DataBindingConfig(R.layout.activity_business_order, BR.vm, mStates)
                .addBindingParam(BR.adapter, orderAdapter);
    }

    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mStates.titleBar.set("我的订单");

        mBinding = (ActivityBusinessOrderBinding) getBinding();

        getLifecycle().addObserver(mAccountRequester);
        initListeners();
        mBinding.smartRefreshLayout.autoRefresh();

        mAccountRequester.getOrderListResult().observe(this, new Observer<ResponseStatus<OrderListBean>>() {
            @Override
            public void onChanged(ResponseStatus<OrderListBean> orderListBeanResponseStatus) {

                if (page == 1) {
                    mBinding.smartRefreshLayout.finishRefresh();
                    mStates.orderList.set(orderListBeanResponseStatus.getData().getList());
                    updateEmptyView(mStates.orderList.get().isEmpty());
                } else {
                    mBinding.smartRefreshLayout.finishLoadMore();

                    List<OrderListBean.ListBean> currentList = mStates.orderList.get();
                    if (currentList != null) {
                        currentList.addAll(orderListBeanResponseStatus.getData().getList());
                        mStates.orderList.set(currentList);
                    }
                }

            }
        });
        mAccountRequester.getSrcSuccess().observe(this, new Observer<ResponseStatus<String>>() {
            @Override
            public void onChanged(ResponseStatus<String> stringResponseStatus) {
                if (stringResponseStatus.getResponseCode() == 0) {
                    ToastUtils.showShortToast(stringResponseStatus.getMsg());
                    mBinding.smartRefreshLayout.autoRefresh();
                }
            }
        });

    }


    private void initListeners() {
        // 下拉刷新
        mBinding.smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

                page++;
                mAccountRequester.getBusinessOrderList(page, "");
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                page = 1;
                mAccountRequester.getBusinessOrderList(page, "");
            }
        });

    }

    private void updateEmptyView(boolean isEmpty) {
        if (isEmpty) {
            mBinding.tvEmpty.setVisibility(View.VISIBLE);
            mBinding.recyclerView.setVisibility(View.GONE);
        } else {
            mBinding.tvEmpty.setVisibility(View.GONE);
            mBinding.recyclerView.setVisibility(View.VISIBLE);
        }
    }

} 